package com.example.demo.controller;

import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResponse;
import com.example.demo.model.ReturnResult;
import com.example.demo.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 添加图书
     */
    @RequestMapping("/addBook")
    public ReturnResult<Boolean> addBook(BookInfo bookInfo){
        log.info("/book/addBook接收到参数 BookInfo: {}", bookInfo);
        // 1. 校验参数
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null){
            return ReturnResult.failure("参数为空", false);
        }
        // 2. 添加图书
        try{
            Integer result = bookService.insertBook(bookInfo);
            if(result > 0){
                return ReturnResult.success(true);
            }else{
                return ReturnResult.failure("添加图书失败", false);
            }
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("内部错误，请联系管理员");
        }
    }

    /**
     * 分页查询
     */
    @RequestMapping("/getListByPage")
    public ReturnResult<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest){
        log.info("/book/getListByPage接收到参数 pageRequest: {}", pageRequest);
        // 1. 参数校验
        if(pageRequest.getCurrentPage() == null){
            return ReturnResult.failure("参数错误", null);
        }
        // 2. 分页查询
        try{
            List<BookInfo> bookList = bookService.selectBookInfoByPage(pageRequest);
            if(bookList == null){
                return ReturnResult.failure("查询异常，请联系管理员", null);
            }
            Integer total = bookService.selectCount();
            return ReturnResult.success(new PageResponse<>(bookList, total, pageRequest));
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("查询异常，请联系管理员");
        }
    }

    /**
     * 查询图书信息
     */
    @RequestMapping("/queryBookById")
    public ReturnResult<BookInfo> queryBookById(Integer bookId){
        log.info("/book/queryBookById接收到参数：bookId: {}", bookId);
        // 1. 参数校验
        if(bookId == null){
            return ReturnResult.failure("参数为空", null);
        }
        // 2. 返回图书信息
        try{
            BookInfo bookInfo = bookService.selectBookById(bookId);
            return ReturnResult.success(bookInfo);
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("内部错误，请联系管理员");
        }
    }

    /**
     * 更新图书信息
     */
    @RequestMapping("/updateBook")
    public ReturnResult<Boolean> updateBook(BookInfo bookInfo){
        log.info("/book/updateBook接收参数 BookInfo: {}", bookInfo);
        // 1. 参数校验
        if(bookInfo.getId() == null){
            return ReturnResult.failure("bookId 为空", false);
        }
        // 2. 更新图书信息
        try{
            Integer result = bookService.updateBookInfoById(bookInfo);
            if(result == 0){
                return ReturnResult.failure("更新图书信息失败",false);
            }
            return ReturnResult.success(true);
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("内部错误，请联系管理员");
        }
    }

    /**
     * 删除图书信息
     */
    @RequestMapping("/deleteBookById")
    public ReturnResult<Boolean> deleteBookInfoById(Integer id){
        log.info("/book/deleteBookById接收参数 id: {}", id);
        // 1. 检验参数
        if(id == null){
            return ReturnResult.failure("参数为空", false);
        }
        // 2. 删除图书
        try{
            Integer result = bookService.deleteBookInfoById(id);
            if(result <= 0){
                return ReturnResult.failure("删除失败", false);
            }
            return ReturnResult.success(true);
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("内部错误，请联系管理员");
        }
    }

    /**
     * 批量删除图书信息
     */
    @RequestMapping("/deleteBookByIds")
    public ReturnResult<Boolean> deleteBookInfoByIds(@RequestParam("ids") List<Integer> ids){
        log.info("/book/deleteBook接收参数 ids: {}", ids);
        // 1. 参数校验
        if(ids == null){
            return ReturnResult.failure("参数错误", false);
        }
        // 2. 执行逻辑删除
        try{
            Integer result = bookService.deleteBookInfoByIds(ids);
            if(result <= 0){
                return ReturnResult.failure("删除失败", false);
            }
            return ReturnResult.success(true);
        }catch(Exception e){
            e.printStackTrace();
            return ReturnResult.error("内部错误，请联系管理员");
        }
    }
}

